// product/product-info?id={id}
import {
  IMaterialResponse,
  IOrderDetailResponse,
  IOrderListResponse,
  IProductCalculatePriceResponse,
  IProductCardListResponse,
  IProductColorSchemesResponse,
  IProductInfoResponse,
  IProductListResponse,
  IProductStylesResponse,
} from '../inteface/product-info'
import { get, post } from '../utils/request'

export const getProductInfo = async (params: {
  productId: string
  packageId?: string
  snapShotId?: string
}): Promise<IProductInfoResponse | undefined> => {
  try {
    const data = await get<IProductInfoResponse>(`/api/product/product-info`, {
      data: {
        id: params.productId,
        packageId: params.packageId,
        snapShotId: params.snapShotId,
      },
    })
    return data
  } catch (error) {
    console.error('获取产品详情失败:', error)
    throw error
  }
}

export const getProductList = async (): Promise<IProductListResponse | undefined> => {
  try {
    const data = await get<IProductListResponse>('/api/product/product-list')
    return data
  } catch (error) {
    console.error('获取产品列表失败:', error)
    throw error
  }
}

// api/product/product-list-with-price
export const getProductListWithPrice = async (
  params?: [{ productId?: number; calculate?: { packageId: number; snapShotId: number } }],
): Promise<IProductCardListResponse | undefined> => {
  try {
    const data = await post<IProductCardListResponse>('/api/product/product-list-with-price', params ? params : [])
    return data
  } catch (error) {
    console.error('获取产品列表失败:', error)
    throw error
  }
}

// /product/calculate-price
export const calculatePrice = async (data: { packageId?: number; productId: number }): Promise<any> => {
  try {
    const res = await get<IProductCalculatePriceResponse>(`/api/product/calculate-price`, { data: data })
    return res
  } catch (error) {
    console.error('计算价格失败:', error)
    throw error
  }
}

//  /update-snapShort
export const updateSnapShort = async (params: any): Promise<any> => {
  try {
    const data = await get<any>(`/api/product/update-snapShort`, params)
    return data
  } catch (error) {
    console.error('更新快照失败:', error)
    throw error
  }
}

// product/obtain-styles
export const obtainStyles = async (): Promise<IProductStylesResponse | undefined> => {
  try {
    const data = await get<IProductStylesResponse>(`/api/product/obtain-styles`)
    return data
  } catch (error) {
    console.error('获取风格失败:', error)
    throw error
  }
}

// product/obtain-color-themes
export const obtainColorThemes = async (): Promise<IProductColorSchemesResponse | undefined> => {
  try {
    const data = await get<IProductColorSchemesResponse>(`/api/product/obtain-color-themes`)
    return data
  } catch (error) {
    console.error('获取色彩方案失败:', error)
    throw error
  }
}

//product/getMaterial
export const getMaterial = async (params: { categoryId: number }): Promise<IMaterialResponse | undefined> => {
  try {
    const data = await get<IMaterialResponse>(`/api/product/getMaterial`, { data: params })
    return data
  } catch (error) {
    console.error('获取材料失败:', error)
    throw error
  }
}

// 更新快照参数类型
export interface UpdateSnapShotParams {
  productId: number
  buildSize: number
  spaceSize?: number
  styleId: number
  colorSchemeId: number
  materialIds: Array<{ categoryId: number; materialId: number }>
}

export const updateSnapShot = async (params: UpdateSnapShotParams): Promise<any> => {
  try {
    const data = await post<any>(`/api/product/update-snapShort`, params)
    return data
  } catch (error) {
    console.error('更新快照失败:', error)
    throw error
  }
}

// order/order-list
export const getOrderList = async (): Promise<any> => {
  try {
    const data = await get<IOrderListResponse>(`/api/order/order-list`)
    return data
  } catch (error) {
    console.error('获取订单列表失败:', error)
    throw error
  }
}

// order/detail
export const getOrderDetail = async (params: { orderNo: string }): Promise<any> => {
  try {
    const data = await get<IOrderDetailResponse>(`/api/order/detail`, { data: params })
    return data
  } catch (error) {
    console.error('获取订单详情失败:', error)
    throw error
  }
}
